#include "ljserver.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

LJServer::LJServer()
{
    QObject::connect(&proxyWindow, SIGNAL(set(bool, QString, QString, QString, QString)), this, SLOT(setProxy(bool, QString, QString, QString, QString)));
}

void LJServer::Login(QString user, QString password)
{
    QString request;

    request = "mode=login&auth_method=clear&user=";
    request = request.append(user);
    request = request.append("&password=");
    request = request.append(password);

    SendRequest(request);
}

void LJServer::Post(QString user, QString password, QString text, QString subj, QString music, QString tags)
{
    QString request;
    QDate date;
    date = date.currentDate();
    QTime time;
    time = time.currentTime();
    QByteArray ba;

    request = "mode=postevent&auth_method=clear&user=";
    request = request.append(user);
    request = request.append("&password=");
    request = request.append(password);
    request = request.append("&event=");
    request = request.append(text.toUtf8().toPercentEncoding());
    request = request.append("&subject=");
    request = request.append(subj.toUtf8().toPercentEncoding());
    request = request.append("&allowmask=0");
    request = request.append("&year=");
    request = request.append(ba.setNum(date.year()));
    request = request.append("&mon=");
    request = request.append(ba.setNum(date.month()));
    request = request.append("&day=");
    request = request.append(ba.setNum(date.day()));
    request = request.append("&hour=");
    request = request.append(ba.setNum(time.hour()));
    request = request.append("&min=");
    request = request.append(ba.setNum(time.minute()));
    request = request.append("&prop_current_music=");
    request = request.append(music.toUtf8().toPercentEncoding());
    request = request.append("&prop_taglist=");
    request = request.append(tags.toUtf8().toPercentEncoding());
    request = request.append("&ver=1");

    SendRequest(request);
}

void LJServer::Refresh(QString user, QString password, int eventCount, bool allEvents)
{
    QString request;
    QByteArray ba;

    request = "mode=getevents&auth_method=clear&user=";
    request = request.append(user);
    request = request.append("&password=");
    request = request.append(password);
    request = request.append("&prefersubject=0");
    request = request.append("&noprops=1");
    request = request.append("&selecttype=lastn");
    request = request.append("&howmany=");
    if (allEvents == TRUE)
        request = request.append("50");
    else
        request = request.append(ba.setNum(eventCount));
    request = request.append("&ver=1");

    SendRequest(request);
}

QNetworkReply *LJServer::SendRequest(QString textRequest)
{
    QNetworkAccessManager *manager = new QNetworkAccessManager;
    QNetworkRequest request(QUrl("http://livejournal.com/interface/flat"));
    manager->setProxy(proxy);
    QByteArray requestString;
    requestString=textRequest.toUtf8();

    reply = manager->post(request,requestString);
    connect(reply, SIGNAL(finished()), this, SLOT(readyReadReply()));
    connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downProgress(qint64,qint64)));

    return reply;
}

void LJServer::setProxy(bool enable,QString hostName, QString port, QString login, QString password)
{
    if (enable)
    {
        proxy.setHostName(hostName);
        proxy.setPort(port.toInt());
        proxy.setUser(login);
        proxy.setPassword(password);
        proxy.setType(QNetworkProxy::HttpProxy);
    }
    if (!enable)
        proxy.setType(QNetworkProxy::NoProxy);
}

void LJServer::readyReadReply()
{
    respondList = QString::fromUtf8(reply->readAll()).split("\n");
    respondListCount = respondList.size()-1;
    emit answerready();
}

void LJServer::downProgress(qint64 receive, qint64 total)
{
    emit progress(receive, total);
}
